# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.jsonUtils_v2.table2Json_YM import Table2JsonYM

import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)


import time
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
"""
       表名：zzh_report_cart

说明：申报车表
"""

class ReportCarDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()


    def selectReportCar(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "SELECT * FROM zzh_report_cart WHERE cart_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr




    def queryReportCarById(self, id):
        """
        根据ID查询信息    查询    zzh_user
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_report_cart WHERE cart_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql,[id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2Json.item_list_to_json(result)
            return total_count , userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

    def queryReportCarByCondition(self, **kwargs):
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        page_index = kwargs['pageIndex']
        PAGE_SIZE = kwargs['pageSize']




        fieldMap = requestFieldMapToDBField_YM.viewItemAndReportCarMap#要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_viewItem_and_reportCar WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT *  FROM view_viewItem_and_reportCar WHERE " + whereSentence
        rs = cur.execute(sql)
        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            if (page_index * PAGE_SIZE) < total_count:
                start = (page_index - 1) * PAGE_SIZE
                end = page_index * PAGE_SIZE
                userOauthPart = result[start: end]
                userOauthJSON = Table2JsonYM.view_ViewItem_and_reportCar_listto_json(userOauthPart)
                return total_count, userOauthJSON
            else:
                start = (page_index - 1) * PAGE_SIZE
                userOauthPart = result[start: ]
                userOauthJSON = Table2JsonYM.view_ViewItem_and_reportCar_listto_json(userOauthPart)
                return total_count, userOauthJSON

        else:
            return 0,None






        """
        条件查询  zzh_user_oauth
        20170525已经不再使用，但是留作参照，万一要改呢
        :param :
        :return:
        此处使用userId为条件查询

        userId = kwargs['userId']
        page_index = kwargs['pageIndex']
        PAGE_SIZE = kwargs['pageSize']

        sql = "SELECT * FROM zzh_report_cart WHERE user_id=%s "
        sql2 = "SELECT item_id FROM zzh_report_cart WHERE uesr_id=%s"
        sql3 = "SELECT * FROM zzh_item WHERE item_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql,[userId])
        itemIds =  cur.execute(sql2,[userId])
        for i in itemIds:
            cur.execute(sql3,[i])


        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            if (page_index * PAGE_SIZE) < total_count:
                start = (page_index - 1) * PAGE_SIZE
                end = page_index * PAGE_SIZE
                userOauthPart = result[start: end]
                userOauthJSON = Table2JsonByYM.report_car_list_to_json(userOauthPart)
                return total_count, userOauthJSON
            else:
                start = (page_index - 1) * PAGE_SIZE
                userOauthPart = result[start: ]
                userOauthJSON = Table2JsonByYM.report_car_list_to_json(userOauthPart)
                return total_count, userOauthJSON

        else:
            cur.close()
            self.conn.close()
            return None

        """



    def insertReportCar(self,Info):
        """

        :param userRechargeInfo:
        :return:
        """

        userId = Info['userId']  #
        itemId = Info['itemId']
        carStatus = Info['carStatus']  #





        cur = self.conn.cursor()
        sql = 'INSERT INTO zzh_report_cart (item_id,user_id, cart_status) VALUES (%s, %s ,%s )'
        for i in itemId:
            rs=cur.execute(sql,[i,userId,carStatus])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None


    def updateReportCar(self,Info):#

        id = Info['id']  # 必填，请传递user_id
        userId = Info['userId']  #
        itemId = Info['itemId']
        carStatus = Info['carStatus']  #


        cur = self.conn.cursor()
        sql = 'UPDATE zzh_report_cart SET item_id=%s,user_id=%s,cart_status=%s  WHERE cart_id=%s'
        rs=cur.execute(sql,[itemId,userId,carStatus,id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None




    def deleteReportCar(self, ids):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组 []
        :return:
        """
        sql = "delete from zzh_report_cart WHERE cart_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs


    def logicDeleteReportCar(self,userId, itemId):
        userId = userId #
        itemId = itemId


        cur = self.conn.cursor()
        sql = 'UPDATE zzh_report_cart SET  cart_status=-1  WHERE item_id=%s  AND user_id=%s'
        for i in itemId:
            rs = cur.execute(sql, [i, userId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None